import org.objectweb.asm.ClassWriter;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;

//public class MyClassLoader extends ClassLoader {
//    byte[] classBytes;
//
//    public MyClassLoader(byte[] classbyte){
//        this.classBytes = classbyte;
//    }
//
//    @Override
//    protected Class findClass (String name) throws ClassNotFoundException {
//
//        String classPath = name.replace(".", "/");
//        InputStream classInputStream = getSystemClassLoader().getResourceAsStream(classPath);
//        try {
//            assert classInputStream != null;
////            byte[] classBytes = new byte[classInputStream.available()];
//            classInputStream.read(classBytes);
//            Class<?> clazz = defineClass(name, classBytes, 0, classBytes.length);
//            resolveClass(clazz);
//            return clazz;
//        } catch (IOException e) {
//            throw new ClassNotFoundException();
//        }
//
//    }
//}


//class MyClassLoader extends URLClassLoader {
//
//    Map<String, byte[]> classBytes = new HashMap<>();
//
//    public MyClassLoader(String name, byte[] b) {
//        super(new URL[0], MyClassLoader.class.getClassLoader());
////        this.classBytes.putAll(classBytes);
//        this.classBytes.put(name,b);
//    }
//
//    @Override
//    protected Class<?> findClass(String name) throws ClassNotFoundException {
//        byte[] buf = classBytes.get(name);
//        if (buf == null) {
//            return super.findClass(name);
//        }
//        classBytes.remove(name);
//        return defineClass(name, buf, 0, buf.length);
//    }
//}



public class MyClassLoader extends ClassLoader
{
    public Class defineClass(String name, byte[] b) {
        return defineClass(name, b, 0, b.length);
    }



    @Override
    protected Class findClass(String name) throws ClassNotFoundException {
        if (name.endsWith("_Stub")) {
            ClassWriter cw = new ClassWriter(0);
            byte[] b = cw.toByteArray();
            return defineClass(name, b, 0, b.length);
        }
        return super.findClass(name);
    }
}